﻿using System;
using System.Collections.Generic;
using System.Web;
using CCPSIP;

namespace CCPSIP
{
    /// <summary>API_068(預定)：iSender帳號刪除API(觸發時機：內線號碼被刪除時)</summary>
    public class delete_user : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            string ApiPassword = "g9ee8d4c-2005-6awb-6kac-g82fc90agp47";

            try
            {
                //【01】取得POST參數
                string Password = req.Form["Password"];
                string PPS_Code = req.Form["PPS_Code"];

                //檢查參數
                if (string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(PPS_Code))
                    throw new Exception("Error_01: Parameter error.");

                //檢查密碼
                if (Password != ApiPassword)
                    throw new Exception("Error_02: API password error.");  //密碼錯誤

                //檢查PPS_CODE
                if (String.IsNullOrEmpty(OracleKitUser.getEmpName(PPS_Code)))
                    throw new Exception("Error_03: PPS code doesn't exist.");

                //【02-1】檢查reg_user資料表
                if (!OracleDataMaintainKit.CheckRegUserExist(PPS_Code))
                    throw new Exception("Error_04: Check reg user table error.");

                List<OracleDataMaintainKit.UserSipNumAndPidInOracleRegUserTable> PidAndSipNumList = OracleDataMaintainKit.GetDisableRegUserPidAndSipNum(PPS_Code);

                //【02-2】檢查外部電話
                foreach (OracleDataMaintainKit.UserSipNumAndPidInOracleRegUserTable u in PidAndSipNumList)
                {
                    if (!IPPbxKitV3.HadUser(u.SipNum))
                        throw new Exception("Error_05: Check IPPBX sip number error.");
                }

                //【02-3】檢查雲端目錄
                foreach (OracleDataMaintainKit.UserSipNumAndPidInOracleRegUserTable u in PidAndSipNumList)
                {
                    if (!CloudDiskKit.HasUserFolder(u.SipNum))
                        throw new Exception("Error_06: Check user cloud disk folder error.");
                }

                //【03-Start】  刪除事務開始
                //【03-1】操作reg_user資料表
                if (!OracleDataMaintainKit.DisableRegUser(PPS_Code, PidAndSipNumList))
                    throw new Exception("Error_07: Update reg user table " + PPS_Code + " failed.");

                //【03-2】刪除外部電話
                foreach (OracleDataMaintainKit.UserSipNumAndPidInOracleRegUserTable u in PidAndSipNumList)
                {
                    if (!IPPbxKitV3.DeleteUser(u.SipNum))
                        throw new Exception("Error_08: Delete external phone number " + u.SipNum + " failed.");
                }

                //【03-3】刪除雲端目錄
                foreach (OracleDataMaintainKit.UserSipNumAndPidInOracleRegUserTable u in PidAndSipNumList)
                {
                    if (!CloudDiskKit.DeleteUserFolder(u.SipNum))
                        throw new Exception("Error_09: Delete user " + u.SipNum + " cloud disk folder failed.");
                }

                //【03-End】刪除事務結束

                //【99】回傳結果
                string ReturnObject = "OK";
                resp.ContentType = "application/json";
                resp.Charset = null;
                fastJSON.JSON.Instance.Parameters.EnableAnonymousTypes = true;
                resultStr = fastJSON.JSON.Instance.ToJSON(ReturnObject);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}